-
Notifications
You must be signed in to change notification settings - Fork 34
Add hash function and re-enable hash tests #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Given that arbs don't compare equal to themselves it probably doesn't make sense to use them in a hashing data structure. The behaviour of list and dict is that they check for (truncated) hash equality first, then identity, then equality. This means that with this PR an inexact arb behaves like a nan: In [1]: import flint
In [2]: a1 = flint.arb(10,1)
In [3]: a2 = flint.arb(10,1)
In [4]: a1 in {a1}
Out[4]: True
In [5]: a1 in {a2}
Out[5]: FalseThe question is whether this is actually useful. Before you mentioned caching and in that sense it can be useful that the lookup sometimes works even if it sometimes fails. What is the actual use case here though? Is it just caching? I wonder if it would make more sense to handle that use case differently. There is a question about what
Some tests already use an I think it also makes sense that all types could have something like a Some types such as matrices and polynomials are mutable and so should probably not have Ideally Python would have some separate idea of equality like |
Oh, that is rough. I agree that isn't desirable.
If I were designing
For my purposes, yes. We have a method that takes (exact) arbs as parameters, and we want to stick a I don't think a |
|
I don't think you can really win either way with the question of structural vs mathematical equality. There will always be people who want or expect one rather than the other. The hash method here is consistent with |
|
Fair point. One other thought: we could keep this hash behavior, but have it raise an error for inexact arbs. If the three options are:
Which do you prefer? I might lean slightly towards 1, since it's still relatively simple, but doesn't have weird edge cases. But I could be persuaded otherwise. |
|
Option 1 seems reasonable. It could be changed to not raises an error later if desired. |
|
Done! |
This has function does not match the behavior of
__eq__- non exact arbs with the same midpoint and radius will hash to the same value even though they're not mathematically equal. I considered having non-exact arbs return random hash values, but that feels wrong (though I do note that the python documentation doesn't explicitly forbid it), and it wouldn't even always work (hash collisions do exist). I also considered hashing the object id for non-exact arbs, but non-exact arbs aren't equal to themselves, so that doesn't quite work correctly either. Overall, this seemed like the simplest solution, even if it does have the possibility to create poor performance if you try to use non-exact arbs in a hashing data structure.